﻿//using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormHzRc : Form
    {

        int ksrow;
        int jsrow;
        public FormHzRc()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FormHzRc_Load(object sender, EventArgs e)
        {
            string sql;
            sql = "select distinct 工序编号,工序名称  from View_工序明细  order by 工序名称 ";
            System.Data.DataTable tb = pub.U8GetTable(sql);
            if (tb != null)
            {
                this.comboBoxRounting.DataSource = tb;
                this.comboBoxRounting.ValueMember = "工序编号";
                this.comboBoxRounting.DisplayMember = "工序名称";
                this.comboBoxRounting.Refresh();
            }

        }

        private void button_search_Click(object sender, EventArgs e)
        {
             if (pub.是否有权限("002001"))
            {
            ListMO();
            }
             else
             {
                 MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
             }
        }

        private void ListMO()
        {
            string BeginDate;
            string EndDate;
            BeginDate = this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd");
            EndDate = this.dateTimePicker_jsrq.Value.ToString("yyyy-MM-dd");
            string sql;
            sql = @"select   '' 行号, 生产订单号, 生产订单号ID, 生产订单行号, 工艺路线ID, 版本代码, 数量, 物料名称, 物料规格, 物料编号, 加工顺序, 工序名称, 
                工序编号ID, 计量单位名称, 计量单位编号, 工序编号,'' 开工日期,''完工日期,生产订单行号,制单日期
FROM      View_工序明细   where 1=1 ";
            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and  制单日期>='" + BeginDate + "' ";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and  制单日期<='" + EndDate + "'";
            }
            if (this.textBox1.Text != "")
            {
                sql += " and 物料名称 like '%" + textBox1.Text + "%' or 物料规格 like '%" + textBox1.Text + "%' or 物料编号 like '%" + textBox1.Text + "%' ";
            }

            sql += " and  工序编号='" + this.comboBoxRounting.SelectedValue.ToString() + "'";

            System.Data.DataTable tb = pub.U8GetTable(sql);
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                    tb.Rows[i]["开工日期"] = pub.Get开工日期by生产订单号ID(tb.Rows[i]["生产订单号ID"].ToString());
                    tb.Rows[i]["完工日期"] = pub.Get完工日期by生产订单号ID(tb.Rows[i]["生产订单号ID"].ToString());
                    ///格子颜色



                }
            }

            //this.dataGridView1
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = tb;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView1.Refresh();
            //for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            //{
            //    DataGridViewRow gr = this.dataGridView1.Rows[i];

            //    if (IsBePrinted(gr.Cells["C生产订单号"].Value.ToString(), gr.Cells["C生产订单行号"].Value.ToString(), gr.Cells["C工艺路线ID"].Value.ToString(), gr.Cells["C物料编号"].Value.ToString(), gr.Cells["C加工顺序"].Value.ToString(), gr.Cells["C工序编号"].Value.ToString()))
            //    {
            //        dataGridView1.Rows[i].Cells["C行号"].Style.BackColor = Color.Red;
            //        dataGridView1.Rows[i].Cells["C生产订单号"].Style.BackColor = Color.Red;
            //        dataGridView1.Rows[i].Cells["C行号"].Style.BackColor = Color.Red;
            //    }
            //}


            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string 生产订单号1 = this.dataGridView1.Rows[i].Cells["C生产订单号"].Value.ToString();
                string 行号 = tb.Rows[i]["生产订单行号"].ToString();

                string sqlbh = " select * from Z_printrecordhz where 单据编号='" + 生产订单号1 + "' and 生产订单行='" + 行号 + "' and 工序编号='" + comboBoxRounting.SelectedValue.ToString() + "' ";

                System.Data.DataTable tbbh = pub.U8GetTable(sqlbh);
                if (tbbh != null)
                {
                    if (tbbh.Rows.Count > 0)
                    {
                        dataGridView1.Rows[i].Cells["C行号"].Style.BackColor = Color.Red;
                    }
                }

               
            }
        }

        private bool IsBePrinted(string 生产订单号, string 生产订单行号, string 工艺路线ID, string 物料编号, string 加工顺序, string 工序编号)
        {
            string sql;
            sql = @"select 生产订单号, 生产订单行号, 工艺路线ID, 物料编号, 加工顺序, 工序编号, printtime, printuser
FROM      Z_HZprintrecord where  1=1  ";
            sql += " and 生产订单号='" + 生产订单号 + "' and 工艺路线ID='" + 工艺路线ID + "' and 物料编号='" + 物料编号 + "' and  加工顺序 ='" + 加工顺序 + "' and  工序编号='" + 工序编号 + "'";
            System.Data.DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            ksrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            jsrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            if (ksrow > jsrow)
            {
                int r;
                r = ksrow;
                ksrow = jsrow;
                jsrow = r;

            }
            for (int i = ksrow; i <= jsrow; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox1.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox2.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }

        private void button_print_Click(object sender, EventArgs e)
        {
             if (pub.是否有权限("002002"))
            {
            int hang = 1;
            int 每页行数 = 10;
            int 当前行=1;
            int 总行数;
            int 起始行 = 5;
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                ///
                当前行 = 0;
                ///
                //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                //Workbook wkbook = excel.Application.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\工序汇总打印.xlsx");
                ////先清空
                //Worksheet wst = (Worksheet)wkbook.Sheets[1];
                //excel.Visible = true;
                //for (int h = 5; h <= 14;h++ )
                //{
                //    for (int lie=1;lie<=11;lie++)
                //    {
                //        wst.Cells[h, lie] = "";
                //    }
                //}
                //wst.Cells[3, 3] = this.comboBoxRounting.SelectedValue.ToString();///工序编号
                //wst.Cells[3, 8] = this.comboBoxRounting.Text ;///工序名称

                //for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                //{
                //    DataGridViewRow gr = this.dataGridView1.Rows[i];
                        
                //        if (this.dataGridView1.Rows[i].Cells[1].Value == null)
                //        {
                //            //不处理
                //        }
                //        else
                //        {
                //            if ((Boolean)gr.Cells[1].Value == true)
                //            {

                //                wst.Cells[当前行+起始行, 1] = gr.Cells[0].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 2] = gr.Cells[2].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 4] = gr.Cells[3].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 3] = gr.Cells[4].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 5] = gr.Cells[5].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 6] = gr.Cells[6].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 7] = gr.Cells[7].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 8] = gr.Cells[8].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 9] = gr.Cells[9].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 10] = gr.Cells[10].Value.ToString();//行号
                //                wst.Cells[当前行+起始行, 11] = gr.Cells[11].Value.ToString();//行号

                //                当前行++;

                //                string sql = "insert into Z_printrecordhz (打印类型,单据编号,打印时间,打印人员,生产订单行,工序编号)";
                //                sql += " values('生产流转卡工序表','" + gr.Cells[2].Value.ToString() + "',getdate(),'" + pub.LoginUser + "','" + gr.Cells[4].Value.ToString() + "','" + comboBoxRounting.SelectedValue.ToString() + "')";
                //                pub.ExcuteSql(sql, "生产流转卡汇总");
                //            }


                //        }
                  
                //}
            }
            }
             else
             {
                 MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
             }
        }
    }
}

 